package com.bridge.bridge_wd.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bridge.bridge_wd.R;
import com.bridge.bridge_wd.bean.CityEntity;
import com.bridge.bridge_wd.databinding.ActivityHeimingdanBinding;
import com.bridge.common.base.BaseActivity;
import com.bridge.common.utils.UltimateBarXUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.yokeyword.indexablerv.IndexableAdapter;

/**
 * @ClassName HeiMingDanActivity
 * @Description TODO消息
 * @Author xushufeng
 * @CreateDate 2021/4/15 0015 21:38
 * @Version 1.0
 * @UpdateDate 2021/4/15 0015 21:38
 * @UpdateRemark 更新说明
 */
public class HeiMingDanActivity extends BaseActivity {
    ActivityHeimingdanBinding binding;
    CityAdapter adapter;
    @Override
    protected int layoutId() {
        return R.layout.activity_heimingdan;
    }

    @Override
    protected void initView() {
        binding = getBind();
        UltimateBarXUtils.setAppBar(this, true, true, R.color.color_f5f5f5);
        // 支持LinearLayoutManager, GridLayoutManager
        binding.indexableLayout.setLayoutManager(new LinearLayoutManager(this));

         adapter = new CityAdapter(this);
        binding.indexableLayout.setAdapter(adapter);
// 排序过程是异步的 另有setDatas(mDatas,IndexCallback callback)  // callback在datas异步排序结束后回调
        List<CityEntity> mCityList = new ArrayList<>();
        mCityList.add(new CityEntity("安徽"));
        mCityList.add(new CityEntity("北京"));
        mCityList.add(new CityEntity("重庆"));
        mCityList.add(new CityEntity("钓鱼岛"));
        mCityList.add(new CityEntity("福建"));
        mCityList.add(new CityEntity("广西"));
        mCityList.add(new CityEntity("广东"));
        mCityList.add(new CityEntity("贵州"));
        mCityList.add(new CityEntity("合肥"));

        mCityList.add(new CityEntity("钓鱼岛"));
        mCityList.add(new CityEntity("福建"));
        mCityList.add(new CityEntity("广西"));
        mCityList.add(new CityEntity("广东"));
        mCityList.add(new CityEntity("贵州"));
        mCityList.add(new CityEntity("合肥"));
        adapter.setDatas(mCityList);
        adapter.setOnItemContentClickListener(new IndexableAdapter.OnItemContentClickListener<CityEntity>() {
            @Override
            public void onItemClick(View v, int originalPosition, int currentPosition, CityEntity entity) {

            }
        });


    }

    @Override
    protected void setListenner() {

    }
    private int provinceIndex = INDEX_INVALID;
    private static final int INDEX_INVALID = 0;
    public class CityAdapter extends IndexableAdapter<CityEntity> {
        private LayoutInflater mInflater;
        Context context;

        public CityAdapter(Context context) {
            this.context = context;
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateTitleViewHolder(ViewGroup parent) {
            View view = mInflater.inflate(R.layout.recycler_view_item_city_title, parent, false);
            return new CityAdapter.IndexVH(view);
        }

        @Override
        public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) {
            View view = mInflater.inflate(R.layout.recycler_view_item_city, parent, false);
            return new CityAdapter.ContentVH(view);
        }

        @Override
        public void onBindTitleViewHolder(RecyclerView.ViewHolder holder, String indexTitle) {
          CityAdapter.IndexVH vh = (CityAdapter.IndexVH) holder;
            vh.tv.setText(indexTitle);
        }

        @Override
        public void onBindContentViewHolder(RecyclerView.ViewHolder holder, CityEntity entity) {
          CityAdapter.ContentVH vh = (CityAdapter.ContentVH) holder;
            vh.tv.setText(entity.getFieldIndexBy());

            if (entity.isChecked()) {
                vh.image.setVisibility(View.VISIBLE);
            } else {
                vh.image.setVisibility(View.GONE);
            }

            boolean checked = provinceIndex != INDEX_INVALID && adapter.getItems().get(provinceIndex).id == entity.id;
            vh.tv.setEnabled(!checked);
        }

        private class IndexVH extends RecyclerView.ViewHolder {
            TextView tv;

            public IndexVH(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv_item);
            }
        }

        private class ContentVH extends RecyclerView.ViewHolder {
            TextView tv;
            ImageView image, head_image;

            public ContentVH(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv_name);
                head_image = (ImageView) itemView.findViewById(R.id.head_image);
                image = (ImageView) itemView.findViewById(R.id.image);
            }
        }
    }
}
